import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'box_demo.dart';
import 'first_demo.dart';

class CommonPage extends StatefulWidget {
  const CommonPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _CommonPageState();
  }
}

class _CommonPageState extends State<CommonPage> {
  int _index = 0;
  final List<Widget> _widgetList = [const FirstDemoPage(title: "首页"), const BoxPage()];
  List<BottomNavigationBarItem> bottomNavItems = [];

  void _onBottomNavigationBarTapped(int index) {
    setState(() {
      _index = index;
    });
  }

  List<BottomNavigationBarItem> _generateBottomNavItems() {
    return [
      _getBottomNavItem('首页', const Icon(CupertinoIcons.home)),
      _getBottomNavItem('盒子', const Icon(CupertinoIcons.home))
    ];
  }

  BottomNavigationBarItem _getBottomNavItem(String title, Icon icon) {
    return BottomNavigationBarItem(icon: icon, label: title);
  }

  @override
  Widget build(BuildContext context) {
    bottomNavItems = _generateBottomNavItems();
    return Scaffold(
      // appBar: AppBar(
      //   title: const Text("Flutter 构建常用的页面框架"),
      // ),
      body: IndexedStack(
        index: _index,
        children: _widgetList,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _index,
        onTap: _onBottomNavigationBarTapped,
        items: bottomNavItems,
      ),
    );
  }
}
